import Vue from 'vue'
import md5 from 'js-md5';
import {
	getUrl
} from "@/utils/login.js";
import {
	getSysTime
} from './getSysTime'
import * as config from '@/common/config.js';
const codeMessage = {
	404: '您所请求的资源无法找到',
	500: '服务器内部错误，无法完成请求',
};
let token = uni.getStorageSync('token'); //凭证
async function service(options = {}) {
	return new Promise(async (resolved, rejected) => {
		let linkUrl = options.url
		options.url = config.CONFIGBASEURL().baseUrl + options.url;
		let guidId = Vue.prototype.$u.guid()
		let areaId = uni.getStorageSync("lifeData").LOCATION && uni.getStorageSync("lifeData").LOCATION
			.areaId || ''
		//配置请求头
		let fromType
		fromType = 'bMiApplet'
		let sysTime = await getSysTime('0', options.url, guidId)
		options.header = {
			"Content-Type": "application/json",
			"shopId": uni.getStorageSync("lifeData").SHOPID || '',
			"memberId": uni.getStorageSync("lifeData").USERINFO && uni.getStorageSync("lifeData")
				.USERINFO.user
				.memberId || '',
			'X-Requested-With': 'XMLHttpRequest',
			'fromType': md5(fromType),
			'sys-time': Date.now() + '@' + guidId,
			'time': sysTime,
			'sign': md5(fromType + ':' + sysTime),
			'Authorization': 'Bearer ' + uni.getStorageSync('token'),
			'version': config.APPVERSION,
			'areaId': areaId
		};
		options.success = (res) => {
			if (Number(res.statusCode) === 200 || Number(res.statusCode) === 500) { //请求成功
				if (res.data.code === 401) {
					Vue.prototype.$u.vuex('TOKEN', '')
					uni.setStorageSync('token', '')
					getUrl()
				} else if (res.data.code === 4000 || res.data.code === 4001 || res.data.code ===
					205) {
					uni.hideLoading()
					uni.showToast({
						icon: 'none',
						duration: 3000,
						title: res.data.msg
					});
					setTimeout(() => {
						uni.reLaunch({
							'url': '/pages/index/index'
						})
					}, 800)

				}  else if (res.data.code === 4002) {
					if (linkUrl == '/framework/platformGoods/list' || linkUrl ==
						'/framework/bPlatformGroup/bInfo') {
						uni.showModal({
							title: '温馨提示',
							content: res.data.msg,
							confirmText: '切换地址',
							confirmColor: "#48A147",
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
									uni.navigateTo({
										url: '/pages/addressLocation'
									})
								} else if (res.cancel) {
									console.log('用户点击取消');
									if (linkUrl == '/framework/bPlatformGroup/bInfo') {
										uni.reLaunch({
											url: '/pages/index/index'
										})
									}
								}
							}
						});
					} else {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							duration: 3000,
							title: res.data.msg
						});
					}
					console.log(linkUrl)

					rejected(res.data); //错误
				} else if (Number(res.statusCode) === 500) {
					// uni.hideLoading()
					uni.showToast({
						icon: 'none',
						duration: 3000,
						title: '系统错误'
					});
				}else {
					resolved(res.data);
				}

			} else {
				uni.showToast({
					icon: 'none',
					duration: 3000,
					title: '系统错误'
				});
				uni.hideLoading()
				rejected(res.data); //错误
			}
		}
		//错误
		options.fail = (err) => {
			rejected(err); //错误
		}
		uni.request(options);

	});
}
export default service;